#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

/*
C语言中通常是怎么处理线程间数据共享的

gcc -o a.out thread.c
./a.out
*/

void *count();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

main()
{
	printf("%s\n", "this is c thread demo");
	int rc1, rc2;
	pthread_t thread1, thread2;

	/* 创建线程，每个线程独立执行函数functionC */
	if((rec1 = pthread_create(&thread1, NULL, &add, NULL)))
	{
		printf("Thread creation failed:%d\n", rc1);
	}
	if((rec2 = pthread_create(&thread2, NULL, &add, NULL)))
	{
		printf("Thread creation failed:%d\n", rc2);
	}
	/* 等待所有线程执行完毕 */
	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);
	exit(0);
}

void *count()
{
	pthread_mutex_lock(&mutex1);
	counter++;
	printf("Counter value:%d\n", counter);
	pthread_mutex_unlock(&mutex1);
}
